package com.laf.manage.controller.user;

import com.laf.common.core.controller.BaseController;
import com.laf.common.core.domain.AjaxResult;
import com.laf.common.core.domain.R;
import com.laf.common.core.domain.entity.MobileUser;
import com.laf.framework.web.service.MobileUserLoginService;
import com.laf.mobile.domain.dto.*;
import com.laf.mobile.service.IMobileUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户管理Controller
 *
 * @author zian
 * @date 2024-10-30
 */
@Slf4j
@Api(tags = "用户管理")
@RestController("userUserController")
@RequestMapping("/user/users")
@RequiredArgsConstructor
public class MobileUserController extends BaseController {

    private final IMobileUserService userService;

    private final MobileUserLoginService mobileUserLoginService;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody @Validated MobileUserLoginDTO dto) {
        log.info("用户登录: " + dto);
        String token = mobileUserLoginService.login(dto.getUsername(), dto.getPassword(), dto.getCode(), dto.getUuid());
        userService.updateLogInfo(dto);
        return AjaxResult.success(token);
    }


    /**
     * 注册
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public R register(@RequestBody @Validated MobileUserRegisterDTO dto) {
        log.info("用户注册 参数有：{}", dto);
        userService.register(dto);
        return R.ok();
    }


    /**
     * 获取用户信息
     *
     * @return
     */
    @GetMapping("/info")
    @ApiOperation("获取用户信息")
    public R getUserInfo() {
        MobileUser user = userService.getUserInfo();
        return R.ok(user);
    }


    /**
     * 更新用户信息
     */
    @PutMapping
    @ApiOperation("更新用户信息")
    public R updateUserInfo(@RequestBody @Validated UpdateMobileUserDTO dto) {
        userService.updateUserInfo(dto);
        return R.ok();
    }


    /**
     * 用户登出
     *
     * @return
     */
    @PostMapping("/quit")
    @ApiOperation("用户登出")
    public R quit(HttpServletRequest request) {
        mobileUserLoginService.quit(request);
        return R.ok();
    }

    /**
     * 注销
     */
    @PostMapping("/logout")
    @ApiOperation("用户注销")
    public R logout(HttpServletRequest request) {
        // 删除对应的消息
        userService.logout();
        // 退出登录
        mobileUserLoginService.quit(request);
        return R.ok();
    }

    /**
     * 找回密码
     */
    @PutMapping("/resetPwd")
    @ApiOperation("找回密码")
    public R resetPwd(@RequestBody ReSetPwdDTO dto) {
        userService.resetPwd(dto);
        return R.ok();
    }


    /**
     * 修改密码
     */
    @PutMapping("/updatePwd")
    @ApiOperation("修改密码")
    public R updatePwd(@RequestBody @Validated UpdatePwdDTO dto, HttpServletRequest request) {
        log.info("修改密码 参数有：{}", dto);
        // 修改密码
        userService.updatePwd(dto);
        // 退出登录
        mobileUserLoginService.quit(request);
        return R.ok();
    }


}
